#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
long long n,x,y,ans=0,f[400005],siz[400005],val[400005];
long long find(long long x)
{
	return f[x]==x?f[x]:f[x]=find(f[x]);
}
int main()
{
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(long long i=1;i<=2*n;i++)
	{
		f[i]=i,siz[i]=1,val[i]=0;
	}
	for(long long i=1;i<=n;i++)
	{
		cin>>x>>y;
		x=find(x),y=find(y);
		if(x!=y)
		{
			f[x]=y;
			siz[y]+=siz[x];
			val[y]+=val[x]+1;
			siz[x]=0,val[x]=0;
		}
		else
		{
			val[x]++;
		}
	}
	for(long long i=1;i<=2*n;i++)
	{
		ans+=min(siz[i],val[i]); 
	}	
	cout<<ans<<endl;
	return 0;
}
